# -*- coding: utf-8 -*- 
"""
@author: fsksf
@project: signal-board
@time: 2021/11/9 15:23
"""
import dataclasses
import datetime
import json
from collections import namedtuple


CodeDetail = namedtuple('CodeDetail', field_names=('code', 'msg'))


class Code:
    OK = CodeDetail(code=20000, msg='ok')
    unable_get_details = CodeDetail(code=50008, msg='Login failed, unable to get user details.')
    error = CodeDetail(code=50000, msg='error.')
    password_incorrect = CodeDetail(code=60204, msg='Account and password are incorrect.')
    repeat_login = CodeDetail(code=60205, msg='Already logged in')
    user_leave = CodeDetail(code=60206, msg='user status is leave')
    user_not_exist = CodeDetail(code=6027, msg='user not exist')
    user_not_login = CodeDetail(code=6028, msg='您未登录或者token已经过期，请重新登陆')
    permission_not_allow = CodeDetail(code=6029, msg='无权限访问此接口')
    data_not_exist = CodeDetail(code=6030, msg='data not exist')
    # 输入错误
    filename_error = CodeDetail(code=7001, msg='上传文件名不符合规则')
    symbol_error = CodeDetail(code=7101, msg='symbol不合法')
    int_error = CodeDetail(7201, msg='输入必须是整数')
    value_repeat_error = CodeDetail(7301, msg='已经存在')


def make_response(code: CodeDetail, data=None):
    if code.code == Code.OK.code:
        return {
            'code': code.code,
            'message': code.msg,
            'data': data
        }
    else:
        return {
            'code': code.code,
            'message': code.msg + str(data) if data else code.msg,
            'data': data
        }


@dataclasses.dataclass
class WebSocketResp:
    msg_type: str
    msg_data: dict

    def __post_init__(self):
        self.dt = datetime.datetime.now()

    def to_dict(self):
        return {
            'msg_type': self.msg_type,
            'msg_data': self.msg_data,
            'dt': self.dt
        }

